﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using compshop.Infrastructure.CompShopDataService;
using Microsoft.Win32;
using System.IO;

namespace compshop.Modules.GeneralProducts.Views
{
    /// <summary>
    /// Interaction logic for AddGeneralProductDialog.xaml
    /// </summary>
    public partial class CreateGeneralProductDialog : Window
    {
        private GeneralProduct genProduct;

        public CreateGeneralProductDialog(GeneralProduct genProduct)
        {
            InitializeComponent();
            this.genProduct = genProduct;
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (genProduct.GeneralProductID != 0)
            {
                InitFields(genProduct);
            }
            else
            {
                cboxCountry.SelectedIndex = 0;
                cboxProducer.SelectedIndex = 1;
                cboxCategory.SelectedIndex = 0;
            }
        }


        private void InitFields(GeneralProduct product)
        {
            for (int i = 0; i < cboxCategory.Items.Count; i++)
            {
                if (((Category)cboxCategory.Items[i]).CategoryID == product.SubCategory.CategoryID)
                {
                    cboxCategory.SelectedIndex = i;
                }
            }

            for (int i = 0; i < cboxSubCategory.Items.Count; i++)
            {
                if (((SubCategory)cboxSubCategory.Items[i]).SubCategoryID == product.SubCategoryID)
                {
                    cboxSubCategory.SelectedIndex = i;
                }
            }

            int index = cboxCountry.Items.IndexOf(product.Country);
            cboxCountry.SelectedIndex = index;

            index = cboxProducer.Items.IndexOf(product.Producer);
            cboxProducer.SelectedIndex = index;

            tboxName.Text = product.Name;
            if (!string.IsNullOrEmpty(product.Info))
            {
                tboxInfo.Text = product.Info;
            }

            if (!string.IsNullOrEmpty(product.BarCode))
            {
                tboxBarcode.Text = product.BarCode;
            }

            tboxPrice.Text = product.CurrentPrice.ToString();

            if (product.Photo != null && product.Photo.Length > 0)
            {
                BitmapImage bi = new BitmapImage();
                MemoryStream ms = new MemoryStream(product.Photo);
                try
                {
                    bi.BeginInit();
                    bi.StreamSource = ms;
                    bi.EndInit();
                    imagePhoto.Source = bi;
                }
                catch (Exception ex)
                {
                }
            }
        }


        private void btnLoadPhoto_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "(Image files|*.jpg;*jpeg;*.bmp;*.gif;*.png" ;
            if (dialog.ShowDialog() == true)
            {
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.UriSource = new Uri(dialog.FileName);
                bi.EndInit();
                imagePhoto.Source = bi;
                imagePhoto.Tag = dialog.FileName;
            }
        }


        private void cboxCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Category category = cboxCategory.SelectedItem as Category;
            cboxSubCategory.ItemsSource = category.SubCategories;
            cboxSubCategory.DisplayMemberPath = "Name";
            if (cboxSubCategory.Items.Count > 0)
                cboxSubCategory.SelectedIndex = 0;
        }


        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(tboxName.Text))
            {
                MessageBox.Show(Resource.EnterNameOfProduct);
                return;
            }

            genProduct.Name = tboxName.Text;

            SubCategory scat = (SubCategory)cboxSubCategory.SelectedItem;
            if (scat == null)
            {
                MessageBox.Show(Resource.NoSelectedSubCategory);
                return;
            }
            genProduct.SubCategoryID = scat.SubCategoryID;
            genProduct.SubCategory = scat;
            scat.GeneralProducts.Add(genProduct);

            Producer prd = (Producer)cboxProducer.SelectedItem;
            if (prd == null)
            {
                MessageBox.Show(Resource.NoSelectedProducer);
                return;
            }

            if (prd.Name == "All")
            {
                MessageBox.Show(Resource.NoCorrectProducerName);
                return;
            }

            genProduct.ProducerID = prd.ProducerID;
            genProduct.Producer = prd;
            prd.GeneralProducts.Add(genProduct);

            Country country = (Country)cboxCountry.SelectedItem;
            if (country == null)
            {
                MessageBox.Show(Resource.NoSelectedCountry);
                return;
            }

            genProduct.CountryID = country.CountryID;
            genProduct.Country = country;
            country.GeneralProducts.Add(genProduct);

            if (!string.IsNullOrEmpty(tboxBarcode.Text))
            {
                genProduct.BarCode = tboxBarcode.Text;
            }

            if (!string.IsNullOrEmpty(tboxInfo.Text))
            {
                genProduct.Info = tboxInfo.Text;
            }

            if (!string.IsNullOrEmpty(tboxPrice.Text))
            {
                decimal price;
                if (decimal.TryParse(tboxPrice.Text, out price))
                {
                    genProduct.CurrentPrice = price;
                }
                else
                {
                    MessageBox.Show(Resource.NoCorrectPriceValue);
                    return;
                }
            }

            if (imagePhoto.Tag != null)
            {
                //string path = ((BitmapImage)productDialog.imagePhoto.Source).UriSource.AbsoluteUri;
                string path = (string)imagePhoto.Tag;

                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                byte [] buff = new byte[fs.Length];
                int len=(int)fs.Length;
                fs.Read(buff, 0, len);
                genProduct.Photo = buff;
                fs.Close();
            }

            DialogResult = true;
            Close();
        }


        private void Window_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            //if (cboxCategory.Items.Count > 0)
            //    cboxCategory.SelectedIndex = 0;
        }




    }
}
